<?php
namespace app\serverfund\service\integral;

use \app\manage\service\ApiBase;
/**
 * @author:      李颖
 * @DateTime:    2017-06-28 11:40:29
 * @Description: 用户积分账户
 */
class IntegralAccount extends ApiBase
{
    protected $integralAccountValidate = '\app\serverfund\validate\IntegralAccount';
    protected $integralAccountLogic = '\app\serverfund\logic\IntegralAccount';
    
    //封装一层检验user_info、opposite_info后再执行stepRun <该页多数方法都需检验这两个json数据>
    protected function _checkJsonThenRun(array $data, string $method, string $scene = ''): array 
    {
        if($data['user_info'] !== '{}'){ $data['user_info'] = json_encode($data['user_info']); }
        if($data['opposite_info'] !== '{}'){ $data['opposite_info'] = json_encode($data['opposite_info']); }

        return $this->stepRun($data, $method, $scene);
    }
    
    /*兑换钱*/
    public function exchange(): array 
    {
        $data = $this->haveIssetReceiveData(['exchange'=>' ', 'money'=>0, 'app_id'=>0, 'currency_scale'=>'']);

        $validate = validate($this->integralAccountValidate);
        if(!$validate->scene('exchange')->check($data)){
            return verify_err(null, $validate->getError());
        }

        //无论是数字币还是人民币去转换积分 统一先转换为人民币后再进行积分转换
        $classPath = '\app\serverfund\logic' . '\\' . 'FundAccount';
        $Obj = new $classPath($data);
        return $this->logic($this->integralAccountLogic)->exchange($Obj, $data);
    }

    # 积分 初始化积分账户
    public function init(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'create_ip'=>' ', 'app_id'=>' ']);
        return $this->stepRun($data, 'init', 'init');
    }

    # 积分 充值 系统->a
    public function prepaid(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'integral'=>'0', 'update_ip'=>' ', 'app_id'=>' ', 'user_info'=>'{}','opposite_info'=>'{}', 'remark'=>'']);
        if(\SYSTEM_ID == $data['user_id']){
            return \verify_err($data, '不能为系统用户充值');
        }
        return $this->_checkJsonThenRun($data, 'prepaid', 'prepaid');
    }

    /*积分充值 系统->a,b,c...*/
    public function prepaidByUids(): array 
    {
        $data = $this->haveIssetReceiveData(['update_ip'=>' ', 'app_id'=>' ', 'user_infos'=>[],'opposite_info'=>'{}']);
        if(count($data['user_infos']) == 0){
            return auto_success(null, '没有满足可以派发积分的用户');
        }
        if($data['opposite_info'] !== '{}'){ $data['opposite_info'] = json_encode($data['opposite_info']); }
        $validate = validate($this->integralAccountValidate);
        if(!$validate->scene('prepaidByUids')->check($data)){
            return verify_err(null, $validate->getError());
        }
        foreach ($data['user_infos'] as $k => $v) {
            if(!isset($v['id'], $v['integral'])){ 
                return verify_err(null, '用户信息与充值积分个数信息不全'); 
            }
            if(\SYSTEM_ID == $v['id']){
                return \verify_err($v, '不能为系统用户充值');
            }
            if(!is_int($v['integral']) || $v['integral'] < 0){
                return verify_err($v, '积分个数错误');
            }
        }
        return $this->logic($this->integralAccountLogic)->prepaidByUids($data);
    }

    # 积分 转账 a->b
    public function transfer(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'integral'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
        return $this->_checkJsonThenRun($data, 'transfer', 'transfer');
    }

    # 积分 冻结 a
    public function freeze(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'integral'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}','type'=>' ','obj_id'=>' ','freeze_counts'=>1]);
        return $this->_checkJsonThenRun($data, 'freeze', 'freeze');
    }

    /*后台管理员冻结某用户积分*/
    public function adminFreeze(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'integral'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}','reason'=>' ']);
        return $this->_checkJsonThenRun($data, 'adminFreeze', '');
    }

    # 积分 解冻 a
    public function unfreeze(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'integral'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'type'=>' ','obj_id'=>' ','freeze_counts'=>' ', 'user_info'=>'{}','opposite_info'=>'{}', 'remark'=>'']);
        return $this->_checkJsonThenRun($data, 'unfreeze', 'unfreeze');
    }

    # 积分 获取列表
    public function list(): array
    {
        $data = $this->haveIssetReceiveData(['page'=>'1', 'list_rows'=>'', 'keywords'=>'']);
        return $this->stepRun($data, 'list', 'list');
    }

    # 积分 某user_ids的用户资金列表
    public function listByUserIds(): array
    {
        $data = $this->haveIssetReceiveData(['user_ids'=>' ']);
        return $this->stepRun($data, 'listByUserIds', 'listByUserIds');
    }

    /*获取某订单实际能抵扣的积分信息*/
    public function getIntegralDeductionInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['user_available_integral'=>0, 'order_max_integral_deduction'=>0, 'order_total_price'=>0, 'order_currency_total_price'=>0, 'fund_type'=>' ', 'app_id'=>0, 'currency_scale'=>'']);
        return $this->stepRun($data, 'getIntegralDeductionInfo');
    }

    /*获取某证书的支付详情 前提为：使用积分进行了抵扣*/
    public function getCertiPayInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['user_available_integral'=>0, 'certi_max_integral_deduction'=>0, 'certi_total_price'=>0, 'app_id'=>0]);
        return $this->stepRun($data, 'getCertiPayInfo');
    }




}